import SafariServices
import UIKit

enum SettingItemType {
  case url
  case page
}

struct SettingItem {
  let title: String
  let subTitle: String?
  let type: SettingItemType
  let link: String
}

struct SettingSection {
  let header: String
  let data: [SettingItem]
}

class SettingViewController: UIViewController {

  private let tableView = UITableView(frame: .zero, style: .insetGrouped)

  let dataSource = [
    SettingSection(
      header: "隐私安全",
      data: [
        SettingItem(
          title: "隐私协议", subTitle: nil, type: .url,
          link: "https://bdw-storage.zmeng123.com/bdw/bdsInput/bds_provacy.html"),
        SettingItem(
          title: "使用手册", subTitle: nil, type: .url,
          link: "https://bdw-storage.zmeng123.com/bdw/bdsInput/bds_use.html"),
        SettingItem(
          title: "个人信息采集清单", subTitle: "见隐私协议", type: .url,
          link: "https://bdw-storage.zmeng123.com/bdw/bdsInput/bds_provacy.html"),
        SettingItem(
          title: "第三方SDK使用信息说明", subTitle: "见隐私协议", type: .url,
          link: "https://bdw-storage.zmeng123.com/bdw/bdsInput/bds_provacy.html"),
      ]),
    SettingSection(
      header: "账号相关",
      data: [
        SettingItem(
          title: "账号注销申请", subTitle: nil, type: .page, link: "AccountDeletionViewController")
      ]),
    SettingSection(
      header: "特别鸣谢",
      data: [
        SettingItem(
          title: "Hamster「仓」输入法", subTitle: nil, type: .page, link: "OpenSourceViewController")
      ]),
    SettingSection(
      header: "其他开源库",
      data: [
        SettingItem(
          title: "librime", subTitle: "https://github.com/rime/librime", type: .url,
          link: "https://github.com/rime/librime"),
        SettingItem(
          title: "KeyboardKit", subTitle: "https://github.com/KeyboardKit/KeyboardKit", type: .url,
          link: "https://github.com/KeyboardKit/KeyboardKit"),
        SettingItem(
          title: "Runestone", subTitle: "https://github.com/simonbs/Runestone", type: .url,
          link: "https://github.com/simonbs/Runestone"),
        SettingItem(
          title: "TreeSitterLanguages", subTitle: "https://github.com/simonbs/TreeSitterLanguages",
          type: .url, link: "https://github.com/simonbs/TreeSitterLanguages"),
        SettingItem(
          title: "ProgressHUD", subTitle: "https://github.com/relatedcode/ProgressHUD", type: .url,
          link: "https://github.com/relatedcode/ProgressHUD"),
      ]),
  ]

  override func viewDidLoad() {
    super.viewDidLoad()
    title = "设置"
    view.backgroundColor = .systemBackground

    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SettingCell")
    view.addSubview(tableView)

    NSLayoutConstraint.activate([
      tableView.topAnchor.constraint(equalTo: view.topAnchor),
      tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
      tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    ])
  }
}

extension SettingViewController: UITableViewDelegate, UITableViewDataSource {

  func numberOfSections(in tableView: UITableView) -> Int {
    return dataSource.count
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataSource[section].data.count
  }

  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return dataSource[section].header
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "SettingCell")
    cell.accessoryType = .disclosureIndicator
    let item = dataSource[indexPath.section].data[indexPath.row]
    cell.textLabel?.text = item.title
    cell.detailTextLabel?.text = item.subTitle
    return cell
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let item = dataSource[indexPath.section].data[indexPath.row]

    switch item.type {
    case .url:
      if let url = URL(string: item.link) {
        let safariVC = SFSafariViewController(url: url)
        present(safariVC, animated: true)
      }
    case .page:
      if item.link == "AccountDeletionViewController" {
        let vc = AccountDeletionViewController()
        vc.title = item.title
        navigationController?.pushViewController(vc, animated: true)
      } else if item.link == "OpenSourceViewController" {
        let vc = OpenSourceViewController()
        vc.title = item.title
        navigationController?.pushViewController(vc, animated: true)
      }
    }
  }
}
